﻿Public Class xtf_LISTADOresumenGeneralSemanal
    Public dts_informe As New dts_informe_1
    Public PERMISOImprimir As String = ""


#Region "Total registros ..."

    Private Sub gdv_general_RowCountChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        bsi_registros.Caption = "Total Registros: " & gdv_general.RowCount
    End Sub

#End Region

#Region "General"
    Private Sub bbi_cerrar_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbi_cerrar.ItemClick
        Me.Close()
    End Sub

    Private Sub btn_Cerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Cerrar.Click
        Me.Close()
    End Sub

    Private Sub bbi_actualizar_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbi_actualizar.ItemClick
        IniciarLista()
    End Sub

    Private Sub xtf_Listado_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        guardar_config_usuariocolumna_()
    End Sub

    Private Sub guardar_config_usuariocolumna_()
        Dim obj_config_usuariocolumna_ As New cls_config_usuariocolumna_
        If obj_config_usuariocolumna_.Buscarxidusuarioxmodulo(UsuarioLOG.idusuario, Me.Name) Then
            obj_config_usuariocolumna_.xml = GetLayoutData(gdv_general)
            obj_config_usuariocolumna_.Modificar()
        Else
            obj_config_usuariocolumna_.xml = GetLayoutData(gdv_general)
            obj_config_usuariocolumna_.idusuario = UsuarioLOG.idusuario
            obj_config_usuariocolumna_.modulo = Me.Name
            obj_config_usuariocolumna_.Nuevo()
        End If
    End Sub

    Public Sub FiltrarColumnas()
        Dim int_IDUsuario As Integer = UsuarioLOG.idusuario
        Dim obj_config_usuariocolumna_ As New cls_config_usuariocolumna_
        gdv_general.BeginUpdate()
        If obj_config_usuariocolumna_.Buscarxidusuarioxmodulo(int_IDUsuario, Me.Name) Then
            SetLayoutData(gdv_general, obj_config_usuariocolumna_.xml)
        End If

        PerzonalizarControl(gdc_general)

        gdv_general.EndUpdate()
    End Sub

    Private Function GetLayoutData(ByRef prm_GridView As DevExpress.XtraGrid.Views.Grid.GridView) As Byte()
        Dim stream As New System.IO.MemoryStream()
        prm_GridView.SaveLayoutToStream(stream)
        Return stream.GetBuffer()
    End Function

    Private Sub SetLayoutData(ByRef prm_GridView As DevExpress.XtraGrid.Views.Grid.GridView, ByVal data As Byte())
        If data Is Nothing OrElse data.Length = 0 Then
            Return
        End If
        Dim stream As New System.IO.MemoryStream(data)
        Try
            prm_GridView.RestoreLayoutFromStream(stream)
        Catch ex As Exception
            Throw New Exception("Wrong data format", ex)
        End Try
    End Sub

    Private Sub bbi_columnas_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbi_columnas.ItemClick
        gdv_general.ColumnsCustomization()
    End Sub
#End Region

    Public Sub New()

        Me.Cursor = Cursors.WaitCursor
        InitializeComponent()
        PERMISOImprimir = "015001002"
        lbl_modulo.Text = "Resumen General Semanal"
        Me.Text = lbl_modulo.Text
        lbl_modulodescripcion.Text = "(detallado por días de la semana)"
        IniciarLista()
        Me.Cursor = Cursors.Default

        dtp_fechaDesde.DateTime = Now

        Dim desde As DateTime = Now
        Dim hasta As DateTime = Now
        dtp_fechaDesde.DateTime = desde


    End Sub

    Public Sub IniciarLista()
        Try
            gdc_general.BeginUpdate()

            gdc_general.DataSource = dts_informe.Tables("cajaasiento__reporte")
            'ulg_Lista.DisplayLayout.GroupByBox.Style = Infragistics.Win.UltraWinGrid.GroupByBoxStyle.Full

            'dtt_DataTable.Columns.Add("Nombre de Fantasía")
            'dtt_DataTable.Columns.Add("Razón Social")
            'dtt_DataTable.Columns.Add("Lunes", GetType(Decimal))
            'dtt_DataTable.Columns.Add("Martes", GetType(Decimal))
            'dtt_DataTable.Columns.Add("Miercoles", GetType(Decimal))
            'dtt_DataTable.Columns.Add("Jueves", GetType(Decimal))
            'dtt_DataTable.Columns.Add("Viernes", GetType(Decimal))
            'dtt_DataTable.Columns.Add("Sabado", GetType(Decimal))
            'dtt_DataTable.Columns.Add("Domingo", GetType(Decimal))

            'Dim obj_Personal_ As New cls_personal_
            'If obj_Personal_.CargarTodos() Then
            '    For Each Personal As cls_personal_ In obj_Personal_.Items
            '        AgregarItem(Personal)
            '    Next
            'End If
            'gdv_general.PopulateColumns()



            gdc_general.EndUpdate()
            FiltrarColumnas()
            'gdv_general.BestFitColumns()
            'gdc_general.EndUpdate()
            'btn_buscar_Click(Nothing, Nothing)
        Catch ex As Exception
            'Dim frmError As New frm_ErrorManager(ex.Message)
        End Try
    End Sub


    Public Sub btn_buscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_buscar.Click

        Try
            Dim dte_lunes As Date = dtp_fechaDesde.DateTime
            Dim dte_sel As Date = dtp_fechaDesde.DateTime

            dts_informe.Tables("cajaasiento__reporte").Clear()

            While Not Format(dte_lunes, "dddd") = "lunes"
                dte_lunes = DateAdd(DateInterval.Day, -1, dte_lunes)
            End While

            Dim obj_command As New SqlClient.SqlCommand()
            Dim obj_Conexion As New cls_Conexion
            obj_Conexion.Conectar()
            With obj_command
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cajaasiento__reporte"

                Dim obj_parametros As New cls_config_parametro_
                .Parameters.Add("@cajaconceptoVentasM1", SqlDbType.Int).Value = obj_parametros.Buscar("cajaconceptoVentasM1").valor
                .Parameters.Add("@cajaconceptoVentasM2", SqlDbType.Int).Value = obj_parametros.Buscar("cajaconceptoVentasM2").valor
                .Parameters.Add("@cajaconceptoVentasM3", SqlDbType.Int).Value = obj_parametros.Buscar("cajaconceptoVentasM3").valor
                .Parameters.Add("@cajaconceptoVentasR1", SqlDbType.Int).Value = obj_parametros.Buscar("cajaconceptoVentasR1").valor
                .Parameters.Add("@cajaconceptoVentasR2", SqlDbType.Int).Value = obj_parametros.Buscar("cajaconceptoVentasR2").valor
                .Parameters.Add("@cajaconceptoVentasR3", SqlDbType.Int).Value = obj_parametros.Buscar("cajaconceptoVentasR3").valor
                .Parameters.Add("@cajaconceptoAporteSocios", SqlDbType.Int).Value = obj_parametros.Buscar("cajaconceptoAporteSocios").valor
                .Parameters.Add("@cajaconceptoProveedores1", SqlDbType.Int).Value = obj_parametros.Buscar("cajaconceptoProveedores1").valor
                .Parameters.Add("@cajaconceptoProveedores2", SqlDbType.Int).Value = obj_parametros.Buscar("cajaconceptoProveedores2").valor
                .Parameters.Add("@cajaconceptoProveedores3", SqlDbType.Int).Value = obj_parametros.Buscar("cajaconceptoProveedores3").valor
                .Parameters.Add("@cajaconceptoSueldos", SqlDbType.Int).Value = obj_parametros.Buscar("cajaconceptoSueldos").valor
                .Parameters.Add("@cajaconceptoRetiroSocios", SqlDbType.Int).Value = obj_parametros.Buscar("cajaconceptoRetiroSocios").valor
                .Parameters.Add("@desde", SqlDbType.DateTime).Value = dte_lunes
                .Parameters.Add("@hasta", SqlDbType.DateTime).Value = DateAdd(DateInterval.Day, 6, dte_lunes)
            End With
            Dim obj_Adapter As New SqlClient.SqlDataAdapter(obj_command)
            gdv_general.BeginDataUpdate()
            obj_Adapter.Fill(dts_informe.Tables("cajaasiento__reporte"))
            gdv_general.EndDataUpdate()

            'For Each col As DevExpress.XtraGrid.Columns.GridColumn In gdv_general.Columns
            '    If col.Caption.Contains(dte_sel.Date.ToShortDateString) Then
            '        col.AppearanceHeader.Font = New Font(col.AppearanceHeader.Font, FontStyle.Bold)
            '        col.AppearanceCell.Font = New Font(col.AppearanceHeader.Font, FontStyle.Bold)
            '    Else
            '        col.AppearanceHeader.Font = New Font(col.AppearanceHeader.Font, FontStyle.Regular)
            '        col.AppearanceCell.Font = New Font(col.AppearanceHeader.Font, FontStyle.Regular)
            '    End If
            'Next

            'Dim report As New xtr_REPORTElistaprecio2
            'report.DataSource = dts_Movimientos

            obj_Conexion.Desconectar()
        Catch ex As Exception
            ErrManagerShow("La configuracion del informe no es correcta.", Me)
        End Try


        'Return report
    End Sub

    Public Sub bbi_imprimir_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbi_imprimir.ItemClick
        If ComprobarPermiso(PERMISOImprimir) Then
            ShowGridPreview(lbl_modulo.Text, gdc_general, False)
        End If
    End Sub

    Public Sub bbi_exportarPDF_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbi_exportarPDF.ItemClick
        If ComprobarPermiso(PERMISOImprimir) Then
            ExportGrid(lbl_modulo.Text, gdc_general, "PDF")
        End If
    End Sub

    Public Sub bbi_vistaprevia_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbi_vistaprevia.ItemClick
        If ComprobarPermiso(PERMISOImprimir) Then
            ShowGridPreview(lbl_modulo.Text, gdc_general, True)
        End If
    End Sub

    Public Sub bbi_exportarExcel_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles bbi_exportarExcel.ItemClick
        If ComprobarPermiso(PERMISOImprimir) Then
            ExportGrid(lbl_modulo.Text, gdc_general, "XLS")
        End If
    End Sub

    Private Sub btn_exportarImprimir_Click(sender As System.Object, e As System.EventArgs) Handles btn_exportarImprimir.Click
        bbi_vistaprevia_ItemClick(Nothing, Nothing)
    End Sub
End Class